package com.example.springbootsfzy.controller.gengminghui;

import com.example.springbootsfzy.domain.Courses;
import com.example.springbootsfzy.service.gengminghui.CourseService;
import com.example.springbootsfzy.util.OSSUtil;
import com.example.springbootsfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
//查询所有课程信息
    @GetMapping("/allCourses")
    public R getAllCourses() {
       List<Courses> courses = courseService.getAllCourses();
        return R.ok(courses);
    }
//通过id查询课程信息
    @GetMapping("/getCourseById")
    public R getCourseById(Integer id) {
        Courses course = courseService.getCourseById(id);
        return R.ok(course);
    }
//通过id删除课程信息
        @DeleteMapping("/deleteById")
      public R deleteByPrimaryKey(Integer id) {
        return R.ok(courseService.deleteByPrimaryKey(id));
    }
    //添加课程信息
    @PostMapping("/insert")
    public R insert(@RequestBody Courses course) {
        return R.ok(courseService.insert(course));
    }
//修改课程信息
    @PutMapping("/updateById")
    public R updateById(@RequestBody Courses course) {
        return R.ok(courseService.updateById(course));
    }

    //分页查询
    @GetMapping("/page")
    public R selectByPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return R.ok(courseService.selectByPage(pageNum, pageSize));
    }

    //分页连表查询所有信息
    @GetMapping("/selectAllInfoPage")
    public R selectAllInfo(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return R.ok(courseService.selectAllInfoPage(pageNum, pageSize));
    }

    //通过课程id查询所有信息
@GetMapping("/selectAllInfoByCourseId")
    public R selectAllInfoByCourseId(Integer courseId) {
        return R.ok(courseService.selectAllInfoPageByCOurseId(courseId));
    }

@GetMapping("/selectAllInfoByconsultantId")
    public R selectAllInfoByconsultantId(Integer consultantId) {
        return R.ok(courseService.selectAllInfoByconsultantId(consultantId));
    }

   @PostMapping("/upload")
    public R upload(MultipartFile[] file) {
       String newname="";
       String path = Paths.get(System.getProperty("user.dir"), "spring-boot-sfzy","src", "main", "resources", "static", "img").toString();

       if (file == null || file.length == 0) {
           return R.error("没有文件被上传");
       }
       File dir = new File(path);
       if (!dir.exists()) {
           dir.mkdirs();// 如果目录不存在，创建目录
           System.out.println("当前的路径:" + path);
       }
       System.out.println("当前的路径:" + path);
       try{
//           为了避免文件重复,需要对文件重新命名,保证文件名的唯一性
           String filename=file[0].getOriginalFilename();
           String ext1=filename.substring(filename.lastIndexOf("."));
           newname= UUID.randomUUID()+ext1;
           System.out.println(newname);
           file[0].transferTo(new java.io.File(path+"\\"+newname));
       }catch (Exception e){
           e.printStackTrace();
           return R.error("上传失败");
       }
       return R.ok(newname);
   }


   //模糊搜索
    @GetMapping("/selectAllInfoLike")
    public R selectAllInfoLike(String courseName,Integer consultantId) {
        return R.ok(courseService.selectByLike(courseName,consultantId));
    }


    //上传视频到oss对象存储
    @PostMapping("/upload2")
    public String upload2(MultipartFile video) throws IOException {
        String originalFilename = video.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + suffix;
        String url= OSSUtil.upload(filename,video.getInputStream());
        return url;
    }

}
